package cc.magicjson.example.demo.task.optional;

public class DefaultSchedulingStrategy implements SchedulingStrategy {
    @Override
    public int compare(PriorityScheduledTask t1, PriorityScheduledTask t2) {
        // EDF: 最早截止期优先
        int edfCompare = Long.compare(t1.getDeadline(), t2.getDeadline());
        if (edfCompare != 0) return edfCompare;

        // RMS: 周期短的优先
        int rmsCompare = Long.compare(t1.getPeriod(), t2.getPeriod());
        if (rmsCompare != 0) return rmsCompare;

        // LLF: 最低松弛度优先
        long laxity1 = t1.getDeadline() - (System.currentTimeMillis() - t1.getCreationTime());
        long laxity2 = t2.getDeadline() - (System.currentTimeMillis() - t2.getCreationTime());
        int llfCompare = Long.compare(laxity1, laxity2);
        if (llfCompare != 0) return llfCompare;

        // 如果以上都相等，则根据重要性排序
        return Integer.compare(t2.getImportance(), t1.getImportance());
    }
}